﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GRWL.BZSC.UI.BaseDataSvc;
using GRWL.BZSC.UI.CustomCtrl;

namespace GRWL.BZSC.UI
{
    public partial class EditPrjtWorkstageFrm : BaseFrm
    {

        public int Id { get; set; }
        public int PrjtId { get; set; }
        public string PrjtCode { get; set; }
        /// <summary>
        /// 当前是否编辑模式
        /// </summary>
        public bool EditMode { get; set; }

        /// <summary>
        /// 当前选中工序
        /// </summary>
        public XPrjtWorkstage PrjtWorkstage { get; set; }

        /// <summary>
        /// 操作状态
        /// </summary>
        Common.BillOperateType _OperateType = Common.BillOperateType.None;
        /// <summary>
        /// 操作状态
        /// </summary>
        Common.BillOperateType OperateType
        {
            get
            {
                return this._OperateType;
            }
            set
            {
                this._OperateType = value;
                initBtns();
            }
        }

        public EditPrjtWorkstageFrm()
        {
            InitializeComponent();
            this.BaseDataProxy = new BaseDataClient();
        }

        private void EditPrjtWorkstage_Load(object sender, EventArgs e)
        {
            this.BaseDataProxy.Open();
            Common.BindOptionComboBox(this.cbBoxType, GRWL.BZSC.Common.EnumCommon.OptionType.加工类型, this.BaseDataProxy, string.Empty);
            this.txtBoxPrjtCode.Text = this.PrjtCode;
            if (this.EditMode && this.Id > 0)
            {
                bindData();
            }
            else
            {
                this.txtBoxSort.Text = this.BaseDataProxy.GetPrdtPrjtSerialNo((int)GRWL.BZSC.Common.EnumCommon.PrdtPrjtSerialNoType.Workstage, this.PrjtId);
                initData();
            }
            bindDgv();
        }

        /// <summary>
        /// 设置窗体按钮显示状态
        /// </summary>
        /// <param name="enable"></param>
        private void setBtnVisible(bool visible)
        {
            this.btnAdd.Visible = visible;
            this.btnSave.Visible = visible;
            this.btnDelete.Visible = visible;
        }

        /// <summary>
        /// 初始化操作按钮状态
        /// </summary>
        private void initBtns()
        {
            this.btnAdd.Enabled = (this.OperateType != Common.BillOperateType.Add) && (this.OperateType != Common.BillOperateType.Update);
            this.btnSave.Enabled = this.OperateType != Common.BillOperateType.View;
            this.btnDelete.Enabled = this.OperateType != Common.BillOperateType.Add;
        }
        
        /// <summary>
        /// 初始化默认数据
        /// </summary>
        private void initData()
        {
            this.txtBoxInNum.Text = "1";
            this.txtBoxOutNum.Text = "1";
            this.txtBoxFixWastage.Text = "0.00";
            this.txtBoxWastageRate.Text = "0.00";
        }

        private void bindData()
        {
            this.PrjtWorkstage = this.BaseDataProxy.GetXPrjtWorkstage(this.Id);

            if (this.PrjtWorkstage != null)
            {
                this.txtBoxFixWastage.Text = this.PrjtWorkstage.PrjtWorkstage.fixWastage.ToString();
                this.txtBoxInNum.Text = this.PrjtWorkstage.PrjtWorkstage.inNum.ToString();
                this.txtBoxOutNum.Text = this.PrjtWorkstage.PrjtWorkstage.outNum.ToString();
                this.txtBoxSort.Text = this.PrjtWorkstage.PrjtWorkstage.sort.ToString();
                this.txtBoxWastageRate.Text = this.PrjtWorkstage.PrjtWorkstage.wastageRate.ToString();
                this.txtBoxWorkstageName.Text = this.PrjtWorkstage.WorkstageName;
                
                
                    SelectItem item = new SelectItem();
                    item.Name = this.PrjtWorkstage.WorkstageName;
                    item.Key = this.PrjtWorkstage.WorkstageCode;
                    item.Value = this.PrjtWorkstage.PrjtWorkstage.workstage.ToString();
                    this.slcrWorkstage.SetSelected(item);
                
                this.cbBoxType.SelectedItem = this.PrjtWorkstage.PrjtWorkstage.type;

                TB_ProductProject pp = this.BaseDataProxy.GetPrdtPrjt(Convert.ToInt32(this.PrjtWorkstage.PrjtWorkstage.project));
                if (pp != null)
                    setBtnVisible(!pp.isSubmit);
            }
        }

        private void slcrWorkstage_SelectorClick(object sender, EventArgs e)
        {
            OptionFrm frm = new OptionFrm();
            frm.IsWorkstage = true;
            frm.Text = "选择工序";
            if (frm.ShowDialog() == DialogResult.OK)
            {
                this.slcrWorkstage.SetSelected(frm.SelectorItem);
                this.txtBoxWorkstageName.Text = frm.SelectorItem.Name;
            }
        }

        /// <summary>
        /// 绑定左边表格
        /// </summary>
        private void bindDgv()
        {
            this.dataGridView1.Rows.Clear();
            XPrjtWorkstage[] ary = this.BaseDataProxy.FindPrjtWorkstages(this.PrjtId);
            if (ary != null)
            {
                int rowId = 0;
                foreach (XPrjtWorkstage p in ary)
                {
                    this.dataGridView1.Rows.Add(new object[] { p.PrjtWorkstage.id, p.PrjtWorkstage.sort, p.WorkstageName, p.PrjtWorkstage.type });
                    if (p.PrjtWorkstage.id == this.Id)
                        this.dataGridView1.Rows[rowId].Selected = true;
                    rowId++;
                }
            }

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            this.EditMode = false;
            this.Id = 0;
            this.cbBoxType.Text = string.Empty;
            this.slcrWorkstage.SetSelected(null);
            this.txtBoxWorkstageName.Text = string.Empty;
            this.txtBoxSort.Text = this.BaseDataProxy.GetPrdtPrjtSerialNo((int)GRWL.BZSC.Common.EnumCommon.PrdtPrjtSerialNoType.Workstage, this.PrjtId);
            initData();
            this.OperateType = Common.BillOperateType.Add;
        }

        

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (this.slcrWorkstage.CurrentSelectItem == null)
            {
                MessageBox.Show("请选择工序！");
                this.slcrWorkstage.Focus();
                return;
            }

            if (string.IsNullOrEmpty(this.txtBoxInNum.Text.Trim()))
            {
                MessageBox.Show("请输入输入数量！");
                this.txtBoxInNum.Focus();
                return;
            }

            if (string.IsNullOrEmpty(this.txtBoxOutNum.Text.Trim()))
            {
                MessageBox.Show("请输入输入数量！");
                this.txtBoxOutNum.Focus();
                return;
            }

            if (string.IsNullOrEmpty(this.txtBoxFixWastage.Text.Trim()))
            {
                MessageBox.Show("请输入固定损耗！");
                this.txtBoxFixWastage.Focus();
                return;
            }

            if (string.IsNullOrEmpty(this.txtBoxWastageRate.Text.Trim()))
            {
                MessageBox.Show("请输入损耗率！");
                this.txtBoxWastageRate.Focus();
                return;
            }


            XPrjtWorkstage xPrjtWorkstage = new XPrjtWorkstage();
            xPrjtWorkstage.PrjtWorkstage = new TB_ProjectWorkstage();
            xPrjtWorkstage.PrjtWorkstage.fixWastage = Convert.ToDecimal(this.txtBoxFixWastage.Text);
            xPrjtWorkstage.PrjtWorkstage.inNum = Convert.ToDecimal(this.txtBoxInNum.Text);
            xPrjtWorkstage.PrjtWorkstage.outNum = Convert.ToDecimal(this.txtBoxOutNum.Text);
            xPrjtWorkstage.PrjtWorkstage.type = this.cbBoxType.Text;
            xPrjtWorkstage.PrjtWorkstage.wastageRate = Convert.ToDecimal(this.txtBoxWastageRate.Text);
            if (this.slcrWorkstage.CurrentSelectItem != null)
            {
                xPrjtWorkstage.PrjtWorkstage.workstage = Convert.ToInt32(this.slcrWorkstage.SelectedValue());
                xPrjtWorkstage.WorkstageCode = this.slcrWorkstage.CurrentSelectItem.Key;
                xPrjtWorkstage.WorkstageName = this.slcrWorkstage.CurrentSelectItem.Name;
            }

            xPrjtWorkstage.PrjtWorkstage.project = this.PrjtId;
            xPrjtWorkstage.PrjtWorkstage.prjtCode = this.PrjtCode;

            bool ok = false;
            Common.OperateType opType = Common.OperateType.Add;
            if (this.EditMode)
            {
                xPrjtWorkstage.PrjtWorkstage.deleteFlag = this.PrjtWorkstage.PrjtWorkstage.deleteFlag;
                xPrjtWorkstage.PrjtWorkstage.id = this.PrjtWorkstage.PrjtWorkstage.id;
                xPrjtWorkstage.PrjtWorkstage.sort = this.PrjtWorkstage.PrjtWorkstage.sort;
                ok = this.BaseDataProxy.UpdatePrjtWorkstage(xPrjtWorkstage.PrjtWorkstage);
                if (ok)
                {
                    if (this.dataGridView1.SelectedRows != null)
                    {
                        this.dataGridView1.SelectedRows[0].SetValues(new object[] { xPrjtWorkstage.PrjtWorkstage.id, xPrjtWorkstage.PrjtWorkstage.sort, xPrjtWorkstage.WorkstageName, xPrjtWorkstage.PrjtWorkstage.type });
                    }
                }
                opType = Common.OperateType.Update;
            }
            else
            {
                xPrjtWorkstage.PrjtWorkstage.sort = Convert.ToInt32(this.txtBoxSort.Text);
                int newId = this.BaseDataProxy.AddPrjtWorkstage(xPrjtWorkstage.PrjtWorkstage);
                xPrjtWorkstage.PrjtWorkstage.id = newId;
                ok = newId > 0;
                if (ok)
                    this.dataGridView1.Rows.Add(new object[] { xPrjtWorkstage.PrjtWorkstage.id, xPrjtWorkstage.PrjtWorkstage.sort, xPrjtWorkstage.WorkstageName, xPrjtWorkstage.PrjtWorkstage.type });
            }

            if (ok)
            {
                this.OperateType = Common.BillOperateType.View;
                CommonEventArg<XPrjtWorkstage> arg = new CommonEventArg<XPrjtWorkstage>(xPrjtWorkstage, opType);
                GRWL.BZSC.Common.EventCommon.OnPrjtWorkstageUdTVEvnt(sender, arg);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dataGridView1.SelectedRows != null)
            {
                this.EditMode = true;
                this.Id = Convert.ToInt32(this.dataGridView1.SelectedRows[0].Cells[0].Value);
                bindData();
                this.OperateType = Common.BillOperateType.None;
            }
        }

        private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            Common.ShowDataGridViewRowNo(this.dataGridView1);
        }

        private void dataGridView1_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
        {
            Common.ShowDataGridViewRowNo(this.dataGridView1);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.SelectedRows != null && this.dataGridView1.SelectedRows.Count > 0 && this.PrjtWorkstage != null)
            {
                if (MessageBox.Show("确认删除该项目吗？", "删除提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    if (this.BaseDataProxy.DeletePrjtWorkstage(this.Id))
                    {
                        this.dataGridView1.Rows.Remove(this.dataGridView1.SelectedRows[0]);
                        CommonEventArg<XPrjtWorkstage> arg = new CommonEventArg<XPrjtWorkstage>(this.PrjtWorkstage, Common.OperateType.Delete);
                        GRWL.BZSC.Common.EventCommon.OnPrjtWorkstageUdTVEvnt(sender, arg);
                    }
                }
            }
        }
    }
}
